﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;
using System.Data.SqlClient;

namespace _3tiers
{
    public partial class form_ThemSuaXoaGiaoVien : Form
    {
        public form_ThemSuaXoaGiaoVien()
        {
            InitializeComponent();
            panel1.Show();
            panel2.Hide();
            panel3.Hide();            
        }
        DataAccess ac = new DataAccess();
        BLL.BLL business = new BLL.BLL();
        private void form_ThemSuaXoaGiaoVien_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'khoaDataSet.tblKhoa' table. You can move, or remove it, as needed.
            this.tblKhoaTableAdapter.Fill(this.khoaDataSet.tblKhoa);
            string sql = "select gv.IDGiaoVien,TenGiaoVien,HocVi,bm.TenBoMon,NgaySinh,SDT,Email from tblGiaoVien gv,tblBoMon bm where gv.IDBoMon=bm.IDBoMon";
            dataGridView1.DataSource = business.ShowData(sql);
            dataGridView2.DataSource = business.ShowData(sql);
            dataGridView3.DataSource = business.ShowData(sql);
        }
        
        private void cbKhoa1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string IDKhoa = cbKhoa1.SelectedValue.ToString();
                string sql = "select * from tblBoMon where IDKhoa='" + IDKhoa + "'";
                DataTable dt = ac.GetTable(sql);
                cbBoMon1.DataSource = dt;
                cbBoMon1.DisplayMember = "TenBoMon";
                cbBoMon1.ValueMember = "IDBoMon";
            }
            catch { };  
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                string IDBoMon = cbBoMon1.SelectedValue.ToString();
                string IDKhoa = cbKhoa1.SelectedValue.ToString();        
                string sql = "insert into tblGiaoVien values('b','" + IDBoMon + "','" + IDKhoa + "','" + tbHoTenGV1.Text + "','" + tbHocVi1.Text + "','" + tbNgaySinh1.Text + "','" + tbSDT.Text + "','" + tbEmail.Text + "')";
                business.Insert(sql);
                label8ThongBao.Text = "Thêm giáo viên thành công.";
            }
            catch { label8ThongBao.Text = "Thêm giáo viên thất bại"; }
            form_ThemSuaXoaGiaoVien_Load(sender, e);
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            tbEmail.Text = "";
            tbHocVi1.Text = "";
            tbHoTenGV1.Text = "";
            tbNgaySinh1.Text = "";
            tbSDT.Text = "";
        }

        //HIển thị panel 1: Thêm giáo viên
        private void radioButtonThemGV_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonThemGV.Checked==true)
            {
                panel1.Show();
                panel2.Hide();
                panel3.Hide();
            }
            else
            {
                
                panel1.Hide();
            }
        }
        
        private void radioButtonSuaGV_CheckedChanged(object sender, EventArgs e)
        {

        }
        //string idKhoa;        
        private void cbKhoa_sua_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Hiển thị mã giáo viên ứng với từng khoa
            try
            {
                string idKhoa = cbKhoa_sua.SelectedValue.ToString();
                string sql = "select * from tblGiaoVien where IDKhoa='" + idKhoa + "'";
                DataTable dt = ac.GetTable(sql);
                cbIDGiaoVien_sua.DataSource = dt;
                cbIDGiaoVien_sua.DisplayMember = "IDGiaoVien";
                cbIDGiaoVien_sua.ValueMember = "IDGiaoVien";
            }
            catch { };  
            //hiển thị bộ môn ứng với từng khoa
            try
            {
                string IDKhoa = cbKhoa_sua.SelectedValue.ToString();
                string sql = "select * from tblBoMon where IDKhoa='" + IDKhoa + "'";
                DataTable dt = ac.GetTable(sql);
                cbBoMon_Sua.DataSource = dt;
                cbBoMon_Sua.DisplayMember = "TenBoMon";
                cbBoMon_Sua.ValueMember = "IDBoMon";
            }
            catch { }; 
        }

        private void radioButtonSuaGV_CheckedChanged_1(object sender, EventArgs e)
        {
            if (radioButtonSuaGV.Checked==true)
            {
                panel1.Hide();
                panel2.Show();
                panel2.SetBounds(13,88,429,562);

            }
            else
            {
                panel2.Hide();
            }
        }
        //string idGiaoVien;       
        private void cbIDGiaoVien_sua_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string idGiaoVien = cbIDGiaoVien_sua.SelectedValue.ToString();
                string sql2 = "select TenGiaoVien from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";                
                string gv = ac.ExcuteScalar(sql2);
                tbTenGiaoVien_Sua.Text = gv;

                string sql3 = "select NgaySinh from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string ns = ac.ExcuteScalar(sql3);
                tbNgaySinh_Sua.Text = ns;

                string sql4 = "select HocVi from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string hv = ac.ExcuteScalar(sql4);
                tbHocVi_Sua.Text = hv;

                string sql5 = "select SDT from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string sdt = ac.ExcuteScalar(sql5);
                tbSDT_Sua.Text = sdt;

                string sql6 = "select Email from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string email = ac.ExcuteScalar(sql6);
                tbEmail_Sua.Text = email;

                string sql7 = "select IDBoMon from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string idbm = ac.ExcuteScalar(sql7);                
                cbBoMon_Sua.SelectedValue = idbm;
                string sql8 = "select * from tblBoMon where IDBoMon='"+idbm+"'";
                string bm = ac.ExcuteScalar(sql8);
                cbBoMon_Sua.DisplayMember = bm;
            }
            catch { }
            //btLichDay_Click(sender, e);
        }

        private void radioButtonXoaGV_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonXoaGV.Checked==true)
            {
                panel1.Hide();
                panel2.Hide();
                panel3.Show();
                panel3.SetBounds(13, 88, 429, 562);
            }
        }
        //Cập nhật thông tin giáo viên.
        private void btSuaGV_Sua_Click(object sender, EventArgs e)
        {
            try
            {
                string IDBoMon = cbBoMon_Sua.SelectedValue.ToString();
                string IDKhoa = cbKhoa_sua.SelectedValue.ToString();                
                string idGiaoVien=cbIDGiaoVien_sua.SelectedValue.ToString();
                string idbm = cbBoMon_Sua.SelectedValue.ToString();
                //MessageBox.Show(IDBoMon+"DFAS"+IDKhoa); 
                string ten=tbTenGiaoVien_Sua.Text;
                string hocvi = tbHocVi_Sua.Text;
                string ns = tbNgaySinh_Sua.Text;
                string sdt = tbSDT_Sua.Text;
                string email = tbEmail_Sua.Text;
                string sql = "update tblGiaoVien set IDBoMon='" + idbm + "',IDKhoa='" + IDKhoa + "',TenGiaoVien='" + ten + "',HocVi='" + hocvi + "',NgaySinh='" + ns + "',SDT='" + sdt + "',Email='" + email + "' where IDGiaoVien='" + idGiaoVien + "'";
                business.UpDate(sql);
                label9ThongBao.Text = "Cập nhật giáo viên thành công.";
            }
            catch { label9ThongBao.Text = "Cập nhật giáo viên thất bại"; }
            form_ThemSuaXoaGiaoVien_Load(sender, e);
            panel2.Show();
        }

        private void btReset_Sua_Click(object sender, EventArgs e)
        {
            tbEmail_Sua.Text = "";
            tbHocVi_Sua.Text = "";
            tbTenGiaoVien_Sua.Text = "";
            tbNgaySinh_Sua.Text = "";
            tbSDT_Sua.Text = "";
        }

        private void cbBoMon_Sua_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void cbBoMon1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbKhoa_Xoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string IDKhoa = cbKhoa_Xoa.SelectedValue.ToString();
                string sql = "select * from tblBoMon where IDKhoa='" + IDKhoa + "'";
                DataTable dt = ac.GetTable(sql);
                cbBoMon_Xoa.DataSource = dt;
                cbBoMon_Xoa.DisplayMember = "TenBoMon";
                cbBoMon_Xoa.ValueMember = "IDBoMon";
            }
            catch { };
        }

        private void cbBoMon_Xoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string idbm = cbBoMon_Xoa.SelectedValue.ToString();
                string sql = "select * from tblGiaoVien where IDBoMon='" + idbm + "'";
                DataTable dt = ac.GetTable(sql);
                cbMaGV_Xoa.DataSource = dt;
                cbMaGV_Xoa.ValueMember = "IDGiaoVien";
                cbMaGV_Xoa.DisplayMember = "IDGiaoVien";
            }
            catch { }; 
        }

        private void cbMaGV_Xoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //tbTenGiaoVien_Sua.Text = "";
                string idGiaoVien = cbMaGV_Xoa.SelectedValue.ToString();
                string sql2 = "select TenGiaoVien from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";               
                string gv = ac.ExcuteScalar(sql2);
                tbHoTenGiaoVien_Xoa.Text = gv;

                string sql3 = "select NgaySinh from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string ns = ac.ExcuteScalar(sql3);
                tbNgaySinh_Xoa.Text = ns;

                string sql4 = "select HocVi from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string hv = ac.ExcuteScalar(sql4);
                tbHocVi_Xoa.Text = hv;

                string sql5 = "select SDT from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string sdt = ac.ExcuteScalar(sql5);
                tbSDT_Xoa.Text = sdt;

                string sql6 = "select Email from tblGiaoVien where IDGiaoVien='" + idGiaoVien + "'";
                string email = ac.ExcuteScalar(sql6);
                tbEmail_Xoa.Text = email;
            }
            catch { }
        }

        private void btXoaGV_Click(object sender, EventArgs e)
        {
            try
            {
                string idgv = cbMaGV_Xoa.SelectedValue.ToString();
                string sql = "delete from tblGiaoVien where IDGiaoVien='" + idgv + "'";
                business.Delete(sql);
                label18.Text = "Xóa thành công";
            }
            catch
            {
                label18.Text = "Xóa không thành công";
            }
            form_ThemSuaXoaGiaoVien_Load(sender, e);
        }
    }
}
